package repository

import (
	"gorm.io/gorm"
	"guliedu/model"
)

type IEduCourseDescriptionRepository interface {
	// 添加描述
	Insert(d *model.EduCourseDescription) error

	// 查询描述
	SelectByCourseID(id string) (*model.EduCourseDescription, error)

	// 更新描述
	Update(*model.EduCourseDescription) error

	// 根据课程ID删除描述
	DeleteByCourseID(id string) error
}

func NewIEduCourseDescriptionRepository() IEduCourseDescriptionRepository {
	return &EduCourseDescriptionRepository{mysqlDb: db}
}

type EduCourseDescriptionRepository struct {
	mysqlDb *gorm.DB
}

func (t *EduCourseDescriptionRepository) Insert(d *model.EduCourseDescription) error {
	return t.mysqlDb.Create(&d).Error
}

func (t *EduCourseDescriptionRepository) SelectByCourseID(id string) (des *model.EduCourseDescription, err error) {
	err = t.mysqlDb.Where("edu_course_id = ?", id).Find(&des).Error
	return
}

func (t *EduCourseDescriptionRepository) Update(dsc *model.EduCourseDescription) error {
	return t.mysqlDb.Save(&dsc).Error
}

func (t *EduCourseDescriptionRepository) DeleteByCourseID(id string) error {
	return t.mysqlDb.Where("edu_course_id = ?", id).Delete(&model.EduCourseDescription{}).Error
}
